<?php 
class ArticleController extends DomController {
	
	public function index() {
		$this->articles();
	}
	/**
	* Creates about form.
	*/
	public function about() {
		
		$panel = array(
			'title' => 'About',
			'html' => '
				This sample shows how to make simple article module with DomAr and DomFg<br />
				
				
				
				<h2>It features:</h2>
				<ul>
					<li> How to make category tree</li>
					<li> How to manage relations between categories and articles</li>
					<li> How to manage images</li>
					<li> How to manage dates </li>
				</ul>
				
				<h2>Taking it apart:<br /></h2>
				Source for forms is in file <strong>application/controllers/Site/ArticleController.php</strong><br />
				DomAr class files are in:
					<ul>
						<li><strong>application/models/Article.domar.php</strong></li>
						<li><strong>application/models/Category.domar.php</strong></li>
					</ul>
				
				<h2>Requirements:</h2>
				<ul>
					<li>Some knowledge about DomAr and how it works!</li>
					<li>Some knowledge about DomSite/DomController and how they work!</li>
				</ul>
				<strong>NB! </strong>Notice that this sample needs database connection. See application/controllers/Site/ArticleController.php method checkSample for connection details.
				It automatically generates database, its tables and some sample data. <br /><br />
				Default connection details:<br />
				<strong>Host</strong>: localhost<br />
				<strong>User</strong>: root<br />
				<strong>Pass</strong>: <none><br />
				<strong>Database</strong>: domfgarticlessample<br />
				
			',
			'bodyStyle' => 'padding:15px',
		);
		
		$this->view->contents = DomFg::render( $panel );
	
	}
	/**
	* Creates categories list/grid and manages deleting categories
	*/
	public function categories() {
		if( !$this->checkSample() ) return; // this just checks database conenction and makes sample data into database
	
		if( $_GET['delete'] ){
			$item = Category::load( $_GET['delete'] );
			if( $item )
				$item->delete();
		}
				
		$source = new DomFgDataSource( Category::getFullCategoryTree(), 999 );
		
		function nrOfArticles( &$row ) {
			return $row['category']->articles->count();
		}
		$panel = array(
			'title' => 'Categories',
			'layout' => 'table',
			'layoutConfig' => array(
				'columns' => 1,
			),
			'items' => array(
				array(
					'type' => 'button',
					'title' => 'Add new category',
					'link' => $this->getUrl('manageCategory/'),
				),
				array(
					'type' => 'grid',
					'source' => $source,
					'fields' => array(
						array('key' => 'value', 'title' => 'Nimi', 'level' => 'level'),
						array('handler' => 'nrOfArticles', 'title' => 'Artikleid'),
						array('type' => 'gridButton', 'value' => 'Muuda', 'link' => $this->getUrl('manageCategory/'), 'title' => 'Valikud','colspan' => 2, 'width'=>90),
						array('type' => 'gridButton', 'value' => 'Kustuta', 'link' => $this->getUrl('categories/&delete=[$id]'), 'confirmMessage' => 'Are you sure?','width'=>90 ),
					),
				),
			),
		);
			
		$this->view->contents = DomFg::render( $panel );

	}
	/**
	* Creates add/change category form
	*/
	public function manageCategory( $id = NULL ) {
		if( !$this->checkSample() ) return; // this just checks database conenction and makes sample data into database
		
		
		$new = $id == NULL;
		$item = $new ? new Category() : Category::load($id);
			
		$post = DomFgPost::getInstance();
		
		if( $post->save ) {
			
			$item->name = $post->name;
			$item->parentCategory = Category::load( $post->parentCategory);
			
			if( $item->save() ) {
				$id = $item->id;
				$message = array(
					'type' => 'ok',
					'colspan' => 2,
					'message' => 'Salvestatud',
				);
			} else {
				$message = array(
					'type' => 'ok',
					'colspan' => 2,
					'message' => 'Tekkinud on viga!',
					'description' => implode('<br>',$item->getValidationReport()->messages),
				);
			}
		}
		
		$panel = array(
			'title' => 'Item',
			'layout' => 'table',
			'action' => $this->getUrl('manageCategory/'.$id),
			'layoutConfig' => array(
				'columns' => 2,
				'columnWidths' => array( 150 ),
			),
			'items' => array(
				$message ,
				array(
					'type' => 'textField',
					'title' => 'Nimi',
					'value' => $item->name,
					'name' => 'name',
				),
				array(
					'type' => 'select',
					'title' => 'Ülem kategooria',
					'values' => Category::getFullCategoryTree( true ),
					'selected' => $item->parentCategory->id,
					'name' => 'parentCategory',
					'property' => 'value',	
					'keyProperty' => 'id',
				),
			),
			'buttons' => array(
				array(
					'title' => 'Salvesta',
					'name' => 'save'
				),
			),
		);
		
		$this->view->contents = DomFg::render( $panel );



	}
	/**
	* Creates articles list/grid and manages deleting articles
	*/
	public function articles() {
		if( !$this->checkSample() ) return; // this just checks database conenction and makes sample data into database

		if( $_GET['delete'] ){
			$item = Article::load( $_GET['delete'] );
			if( $item )
				$item->delete();
		}
				
		$source = new DomArDataSource( Article::query()->orderBy('t.time DESC'), 8 );
		
		$panel = array(
			'title' => 'Items',
			'layout' => 'table',
			'layoutConfig' => array(
				'columns' => 1,
			),
			'items' => array(
				array(
					'type' => 'button',
					'title' => 'Lisa uus',
					'link' => $this->getUrl('manageArticle/'),
				),
				array(
					'type' => 'grid',
					'source' => $source,
					'fields' => array(
						array('key' => 'title', 'title' => 'Pealkiri'),
						array('key' => array('category','name'), 'title' => 'Kategooria'),
						array('key' => 'time', 'title' => 'Aeg'),
						array('type' => 'gridButton', 'value' => 'Muuda', 'link' => $this->getUrl('manageArticle/'), 'title' => 'Valikud','colspan' => 2),
						array('type' => 'gridButton', 'value' => 'Kustuta', 'link' => $this->getUrl('articles/&delete=[$id]'), 'confirmMessage' => 'Are you sure?' ),
					),
				),
			),
		);
			
		$this->view->contents = DomFg::render( $panel );



	}
	/**
	* Creates add/change article form
	*/
	public function manageArticle( $id = NULL ) {
		if( !$this->checkSample() ) return; // this just checks database conenction and makes sample data into database

		$new = $id == 0;
		$item = $new ? new Article() : Article::load($id);
			
		$post = DomFgPost::getInstance();
		
		if( $post->save ) {
			
			$item->title = $post->title;
			$item->time = $post->time;
			$item->introduction = $post->introduction;
			$item->contents = $post->contents;
			$item->image->addUploaded( $post->image[0], $post->image[1] );
			
			$item->category  = Category::load( $post->category );
			
			if( $item->save() ) {
				$id = $item->id;
				$message = array(
					'type' => 'ok',
					'colspan' => 5,
					'message' => 'Salvestatud',
				);
			} else {
				$message = array(
					'type' => 'ok',
					'colspan' => 5,
					'message' => 'Tekkinud on viga!',
					'description' => implode('<br>',$item->getValidationReport()->messages),
				);
			}
		}
		
		$panel = array(
			'title' => 'Article',
			'layout' => 'table',
			'action' => $this->getUrl('manageArticle/'.$id),
			'layoutConfig' => array(
				'columns' => 5,
				'columnWidths' => array( 100,100,230,100 ),
			),
			'items' => array(
				$message ,
				array(
					'type' => 'image',
					'style' => 'width:100px; height:100px',
					'src' => $item->icon->getUrl(),
					'rowspan' => 3,
				),
				array(
					'type' => 'textField',
					'title' => 'Pealkiri',
					'value' => $item->title,
					'name' => 'title',
				),
				array(
					'type' => 'dateTime',
					'title' => 'Aeg',
					'value' => $item->time,
					'name' => 'time',
				),
				array(
					'type' => 'select',
					'title' => 'Kategooria',
					'values' => Category::getFullCategoryTree( true ),
					'selected' => $item->category->id,
					'name' => 'category',
					'property' => 'value',	
					'keyProperty' => 'id',
				),
				array(
					'type' => 'file',
					'title' => 'Pildi fail',
					'name' => 'image',
				),
				array(
					'type' => 'textArea',
					'title' => 'Sissejuhatus',
					'value' => $item->introduction,
					'name' => 'introduction',
					'style' => 'width:600px',
					'colspan' => 4,
				),
				
				array(
					'type' => 'htmlEditor',
					'title' => 'Sisu',
					'value' => $item->contents,
					'name' => 'contents',
					'colspan' => 4,
				),
			),
			'buttons' => array(
				array(
					'title' => 'Salvesta',
					'name' => 'save'
				),
			),
		);
		
		$this->view->contents = DomFg::render( $panel );


	}
	/**
	* Checks/Creates database connection and generates some sample data.
	*/
	public function checkSample() {

		try {
			$item = Category::query()->limit(1)->commit();
			return true;
		} catch( Exception $e ) {
			// continue creating tables.
		}
		
		set_time_limit(60);
		Category::getTable()->update();
		
		$c1 = new Category();
		$c1->name = 'Articles';
		
		$c2 = new Category();
		$c2->name = 'People';
		$c2->parentCategory = $c1;
		
		$c3 = new Category();
		$c3->name = 'Life';
		$c3->parentCategory = $c1;
		
		$c4 = new Category();
		$c4->name = 'Fashion';
		$c4->parentCategory = $c1;
		
		
		$c5 = new Category();
		$c5->name = 'News';
		
		$c6 = new Category();
		$c6->name = 'Estonia';
		$c6->parentCategory = $c5;
		
		$c7 = new Category();
		$c7->name = 'World';
		$c7->parentCategory = $c5;
		
		
		
		$a = new Article();
		$a->title = 'First peoples article';
		$a->introduction = 'Some introduction';
		$a->contents = 'Some contents';
		$a->time  = '2008-09-01 10:00:00';
		$a->category = $c2;
		
		$a = new Article();
		$a->title = 'Second peoples article';
		$a->introduction = 'Some introduction';
		$a->contents = 'Some contents';
		$a->time  = '2008-09-02 10:00:00';
		$a->category = $c2;
		
		$a = new Article();
		$a->title = 'First Life article';
		$a->introduction = 'Some introduction';
		$a->contents = 'Some contents';
		$a->time  = '2008-09-04 10:00:00';
		$a->category = $c3;
		
		
		$a = new Article();
		$a->title = 'News about Estonia 1';
		$a->introduction = 'Some introduction';
		$a->contents = 'Some contents';
		$a->time  = '2008-09-04 11:00:00';
		$a->category = $c6;
		
		$a = new Article();
		$a->title = 'News about Estonia 2';
		$a->introduction = 'Some introduction';
		$a->contents = 'Some contents';
		$a->time  = '2008-09-05 10:00:00';
		$a->category = $c6;
		
		$a = new Article();
		$a->title = 'News about Estonia 3';
		$a->introduction = 'Some introduction';
		$a->contents = 'Some contents';
		$a->time  = '2008-09-06 10:00:00';
		$a->category = $c6;
		
		$a = new Article();
		$a->title = 'News about World 1';
		$a->introduction = 'Some introduction';
		$a->contents = 'Some contents';
		$a->time  = '2008-09-07 10:00:00';
		$a->category = $c7;
		
		$a = new Article();
		$a->title = 'News about World 2';
		$a->introduction = 'Some introduction';
		$a->contents = 'Some contents';
		$a->time  = '2008-09-07 12:00:00';
		$a->category = $c7;
		
		$a = new Article();
		$a->title = 'News about World 3';
		$a->introduction = 'Some introduction';
		$a->contents = 'Some contents';
		$a->time  = '2008-09-08 10:00:00';
		$a->category = $c7;
		
		
		$c1->save();
		$c5->save();

		return true;
	}
	
	
}
?>